﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Zhy.BMS.Base;
using Zhy.BMS.Models;

namespace Zhy.BMS.ViewModels
{
    public class MainViewModel : NotifyBase
    {
        public static List<MainModel> arg = new List<MainModel>();

        #region Monitor页面的数据
        /// <summary>
        /// 电池组数据
        /// </summary>
        public List<string> BatteryGroup { get; set; } = new List<string>();
        public List<string> BatteryMessage { get; set; } = new List<string>();
        #endregion

        #region 趋势图表页面的数据
        public ObservableCollection<ObservableCollection<double>> ChartValues { get; set; } = new ObservableCollection<ObservableCollection<double>>();
        #endregion

        // 呈现页面对象   ，之前是页面地址（不使用）
        private object _pageObject;
        /// <summary>
        /// 内容页面
        /// </summary>
        public object PageObject
        {
            get { return _pageObject; }
            set { SetProperty(ref _pageObject, value); }
        }

        /// <summary>
        /// 菜单按钮事件
        /// </summary>
        public Command NavCommand { get; set; }

        public MainViewModel()
        {
            // 菜单按钮切换内容事件
            NavCommand = new Command(SwitchPage);
            this.SwitchPage("MonitorPage"); // 初始化第一个内容页面
            // 模拟电池组数据
            for (int i = 0; i < 100; i++)
            {
                BatteryGroup.Add(i.ToString());
            }

            for (int i = 0; i < 25; i++)
            {
                BatteryMessage.Add(i.ToString());
            }

            Random random = new Random();
            for (int i = 0; i < 3; i++)// 3条序列
            {
                ObservableCollection<double> temp = new ObservableCollection<double>();
                for (int j = 0; j < 20; j++)// 每个序列2000个点
                {
                    temp.Add(random.NextDouble() * 100);
                }
                ChartValues.Add(temp);
            }

            Task.Run(async () =>
            {
                while (true)
                {
                    await Task.Delay(1000);
                    for (int i = 0; i < 3; i++)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            ChartValues[i].Add(random.NextDouble() * 100);// 这个变化是会导致哪个触发
                        });
                    }
                }
            });
        }

        /// <summary>
        /// 通过反射实例化内容页面
        /// </summary>
        /// <param name="page"></param>
        private void SwitchPage(object page)
        {
            // 通过反射实例化内容页面
            Type type = Assembly.GetExecutingAssembly().GetType("Zhy.BMS.Views.Pages." + page.ToString())!;
            // 这里的!表示我确定值不会为空
            this.PageObject = Activator.CreateInstance(type)!;
        }

    }
}
